﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BrainMatch
{
    public class WaveItem
    {
        private double[]        fingerData=new double[13];
        public double[] FingerData
        {
            get
            {
                return fingerData;
            }
        }

        private PointF          minPoint = new PointF(9999, 9999);
        public PointF MinPoint
        {
            get
            {
                return minPoint;
            }
        }
        private PointF          maxPoint = new PointF(-9999, -9999);
        public PointF MaxPoint
        {
            get
            {
                return maxPoint;
            }
        }
        private GraphicsPath    graphics = null;
        public GraphicsPath Wave
        {
            get
            {
                return graphics;
            }
        }
        public string WaveName {get;set;}
        public string WaveType { get; set; }
        public int TypeID { get; set; }


        private void CheckPoint(PointF pt)
        {
            if (pt.X > maxPoint.X) maxPoint.X=pt.X;
            if (pt.Y > maxPoint.Y) maxPoint.Y=pt.Y;
            if (pt.X < minPoint.X) minPoint.X = pt.X;
            if (pt.Y < minPoint.Y) minPoint.Y = pt.Y;
        }
        public WaveItem(int pos, int param, byte[] data,string wtype)
        {
            WaveName = wtype;

            SByte[] data2 = new SByte[3001];
            double[] dData = new double[10];
            double[] dStack = new double[258];

            double beishu = 1;
            double arg1 = 0;
            double arg2 = 0;

            //绘制圆形图	
            Debug.WriteLine(string.Format("\nHL------->{0}\n",pos));
            int i = 1;
            SByte[] pdata1 = new SByte[data.Length + 1];
            
            for (i = 0; i < data.Length; i++)
            {
                //if (data[i] > 127)
                //    pdata1[i] = data[i] - 256;
                //else
                    pdata1[i] = (SByte)data[i];
            }
            
            i = 1;
            do
            {
                data2[i] =(SByte) (pdata1[16 * i + pos - 1] - 80);
                i++;
            } while (i != 3001);
            

            i = 1;
            do
            {
                data2[i] =(SByte)((data2[i + 1] + data2[i + 2] + data2[i + 3] + data2[i + 4] + data2[i + 5] + data2[i + 6] + data2[i + 7]) / 7);//求平均

                ++i;
            } while (i != 2991);
            data2[0] = data2[1];

            int v49 = 0;
            int j = 0;
            int v10 = param / 9;
            do
            {
                j = -128;
                do
                {
                    int v12 = j + 129;
                    int k = 0;
                    int v13 = v10 * v49 + 1;
                    int v14 = v10 + v10 * v49;
                    int v11 = v14 - v13;
                    if (v11 > 0)
                    {
                        int v17 = v11 + 1;
                        int v16 = (v10 * v49 + 1);//data2+(v10*v49+1);
                        do
                        {
                            if (data2[v16] >= j)
                                if (data2[v16 - 1] < j)
                                    k++;
                            if (data2[v16] <= j)
                                if (data2[v16 - 1] > j)
                                    k++;
                            v16++;
                            v17--;
                        } while (v17 > 0);
                    }
                    dStack[j + 129] = k;
                    k = 0;
                    j++;
                } while (j != 129);
                j = 1;
                double v48 = 0.0;
                do
                {
                    if (v48 < dStack[j])
                        v48 = dStack[j];
                    j++;
                } while (j != 258);
                v48 *= 0.35;
                dStack[0] = 0.0;
                int v18 = 0;
                j = 1;
                do
                {
                    if (dStack[j] > v48)
                    {
                        dStack[0] += dStack[j];
                        v18++;
                    }
                    j++;
                } while (j != 258);
                dData[v49] = dStack[0] / ((double)v18 + 0.000001);

                
                v49++;
            } while (v49 != 9);

            dData[1] += 0.0001;
            dData[2] += 0.0001;
            dData[3] += 0.0001;
            dData[4] += 0.0001;
            dData[5] += 0.0001;
            dData[6] += 0.0001;
            dData[7] += 0.0001;
            dData[8] += 0.0001;
            double v29 = dData[1];
            double v28 = dData[1];
            int v2 = 9;
            int v1 = 1;// dData+1;
            do
            {
                if (dData[v1] > v29)
                    v29 = dData[v1];
                if (dData[v1] < v28)
                    v28 = dData[v1];

                Debug.Write(dData[v1]);
                Debug.Write(",");

                ++v1;
                --v2;
            }
            while (v2 != 0);
            double v31 = v29 * v29 - v28 * v28;
            double v30 = v28 * v28;
            double v40 = (dData[1] * dData[1] - v30) / v31 * 10.0;
            double v39 = (dData[2] * dData[2] - v30) / v31 * 10.0;
            double v38 = (dData[3] * dData[3] - v30) / v31 * 10.0;
            double v37 = (dData[4] * dData[4] - v30) / v31 * 10.0;
            double v36 = (dData[5] * dData[5] - v30) / v31 * 10.0;
            double v35 = (dData[6] * dData[6] - v30) / v31 * 10.0;
            double v34 = (dData[7] * dData[7] - v30) / v31 * 10.0;
            double v33 = (dData[8] * dData[8] - v30) / v31 * 10.0;
            double v32 = (dData[9] * dData[9] - v30) / v31 * 10.0;
            double v41 = 0.0;
            double v44 = 1.0;
            double v45 = 1.0;

            int v3 = 0;
            double v27, v26;
            double v43;
            double v42;
            while (v41 < 10000.0)
            {
                v27 = Math.Sin(v34 * v41 + 2.355) * v40 * v40;
                v26 = Math.Sin(v33 * v41) * v39 * v39 + (double)v27;
                v43 = Math.Sin(v32 * v41 + 1.57) * v38 * v38 + (double)v26;

                v27 = Math.Cos(v34 * v41 + 2.355) * v37 * v37;
                v26 = Math.Cos(v33 * v41) * v36 * v36 + (double)v27;
                v42 = Math.Cos(v32 * v41 + 1.57) * v35 * v35 + (double)v26;

                if (Math.Abs(v43) > v45)
                {
                    v45 = Math.Abs(v43) + 5.0;
                    v3 = 0;
                }
                if (Math.Abs(v42) > v44)
                {
                    v44 = Math.Abs(v42) + 5.0;
                    v3 = 0;
                }
                ++v3;
                if (v3 > 0xDAC)
                    break;
                v41 = v41 + 0.008;
            }
            v41 = 0.0;
            v27 = Math.Sin(v34 * 0.0 + 2.355) * v40 * v40;
            v26 = Math.Sin(v33 * v41) * v39 * v39 + v27;
            v43 = Math.Sin(v32 * v41 + 1.57) * v38 * v38 + v26;
            v27 = Math.Cos(v34 * v41 + 2.355) * v37 * v37;
            v26 = Math.Cos(v33 * v41) * v36 * v36 + (double)v27;
            v42 = Math.Cos(v32 * v41 + 1.57) * v35 * v35 + v26;
            v43 = (v43 / v45 * 135.0 + 154.0) / beishu + (double)arg1;
            v42 = (v42 / v44 * 135.0 + 154.0 + 145.0) / beishu + (double)arg2;

            //Math.Sink->BeginFigure(CD2DPointF(Frndint(v42),Frndint(v43)),D2D1_FIGURE_BEGIN_FILLED);


           // graphics=new GraphicsPath();

            //PointF lastPoint=new PointF((float)v42,(float)v43);//dc.MoveTo(Frndint(v42),Frndint(v43));
            //CheckPoint(lastPoint);
            //v41 = 0.0;
            //double v4 = 0;


            //while (v41 < 10000.0)
            //{
            //    v27 = Math.Sin(v34 * v41 + 2.355) * v40 * v40;
            //    v26 = Math.Sin(v33 * v41) * v39 * v39 + (double)v27;
            //    v43 = Math.Sin(v32 * v41 + 1.57) * v38 * v38 + (double)v26;
            //    v27 = Math.Cos(v34 * v41 + 2.355) * v37 * v37;
            //    v26 = Math.Cos(v33 * v41) * v36 * v36 + (double)v27;
            //    v42 = Math.Cos(v32 * v41 + 1.57) * v35 * v35 + (double)v26;


            //    v43 = (v43 / v45 * 135.0 + 154.0) / beishu + (double)arg1;
            //    v42 = (v42 / v44 * 135.0 + 154.0 + 145.0) / beishu + (double)arg2;

            //    //dc.LineTo(Frndint(v42),Frndint(v43));
            //    PointF pt=new PointF((float)v42,(float)v43);
            //    CheckPoint(pt);
            //    //Math.Sink->AddLine(CD2DPointF(Frndint(v42),Frndint(v43)));
            //    graphics.AddLine(lastPoint,pt);
            //    lastPoint=pt;
            //    ++v4;
            //    if (v4 > 0xDAC)
            //        break;
            //    v41 += 0.008;
            //}

            double[] ps = { v42,v43,v34, v40, v33, v39, v32, v38, v37, v36, v35,v45, v44 };
            Array.Copy(ps, fingerData, 13);
            graphics = MakePath(fingerData);
        }
        public WaveItem(double[] data)
        {
            Array.Copy(data, fingerData, 13);
            graphics = MakePath(fingerData);
        }
        private GraphicsPath MakePath(double[] param)
        {
            GraphicsPath p = new GraphicsPath();
            double v41 = 0.0;
            double v27 = 0.0;
            double v26 = 0.0;
            double v4 = 0.0;

            PointF lastPoint = new PointF((float)param[0], (float)param[1]);//dc.MoveTo(Frndint(param[0]),Frndint(param[1]));
            CheckPoint(lastPoint);
            
            while (v41 < 10000.0)
            {
                v27 = Math.Sin(param[2] * v41 + 2.355) * param[3] * param[3];
                v26 = Math.Sin(param[4] * v41) * param[5] * param[5] + (double)v27;
                param[1] = Math.Sin(param[6] * v41 + 1.57) * param[7] * param[7] + (double)v26;
                v27 = Math.Cos(param[2] * v41 + 2.355) * param[8] * param[8];
                v26 = Math.Cos(param[4] * v41) * param[9] * param[9] + (double)v27;
                param[0] = Math.Cos(param[6] * v41 + 1.57) * param[10] * param[10] + (double)v26;

                param[1] = (param[1] / param[11] * 135.0 + 154.0);
                param[0] = (param[0] / param[12] * 135.0 + 154.0 + 145.0);

                //dc.LineTo(Frndint(param[0]),Frndint(param[1]));
                PointF pt = new PointF((float)param[0], (float)param[1]);
                CheckPoint(pt);

                p.AddLine(lastPoint, pt);
                lastPoint = pt;
                ++v4;
                if (v4 > 0xDAC)
                    break;
                v41 += 0.008;
            }
            return p;
        }
    }
}
